<template>
  <div id="maskIndex">
    <el-row>
      <!-- 表格查询条件 -->
      <el-col :span="24">
        <el-form :inline="true" ref="stationForm" :model="queryForm">
          <el-form-item>
            <el-input v-model="queryForm.name" placeholder="模型名称"></el-input>
          </el-form-item>
          <el-form-item>
            <gd_param type="select"
                      paramCode="gptMaskType"
                      placeholder="指令类型"
                      v-model="queryForm.gptMaskTypeCode" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getTableDatas()">查询</el-button>
            <el-button @click="resetQueryForm" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <!-- 数据表格 -->
      <el-col :span="24">
        <el-row>
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-plus" @click="dialog.saveOrEditDiv=true">新增面具</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="moreDeletes">
              <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除面具</el-button>
            </el-popconfirm>
          </el-col>
        </el-row>
        <el-row style="margin-top:10px;">
          <el-col :span="24">
            <el-table :data="tableData" border style="width: 100%" @selection-change="tableDataSelected">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="name" label="面具名称"></el-table-column>
              <el-table-column prop="gptMaskTypeName" label="面具类型"></el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="180">
                <template slot-scope="scope">
                  <el-button @click="rowShow(scope.row)" size="small" icon="el-icon-search" circle title="查看"></el-button>
                  <el-button @click="rowEdit(scope.row)" type="primary" size="small" icon="el-icon-edit" circle title="编辑"></el-button>
                  <el-button @click="rowDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="tableSizeChange"
              @current-change="tableCurrentChange"
              :current-page="pageData.page"
              :page-sizes="[10, 20, 40, 100]"
              :page-size="pageData.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageData.total"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <!-- 新增\修改用户信息弹窗 -->
    <el-dialog
      :title="(dialog.isEdit == true ? '编辑' : '新增') + '面具'"
      :visible.sync="dialog.saveOrEditDiv"
      :close-on-click-modal="false"
      :show-close="false"
      width="70%"
      :destroy-on-close="true"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData"
        label-width="110px"
      >
        <el-form-item label="面具名称" prop="name">
          <el-input
            v-model="saveOrEditData.name"
            placeholder="请填写面具名称"
            maxlength="80"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="指令类型" prop="gptMaskTypeCode">
          <gd_param type="select"
                    paramCode="gptMaskType"
                    placeholder="指令类型"
                    @selectChange="gptMaskTypeChange"
                    v-model="saveOrEditData.gptMaskTypeCode" />
        </el-form-item>
        <el-form-item label="指令内容" prop="toolDesc">
          <el-input
            type="textarea"
            placeholder="请输入指令内容"
            v-model="saveOrEditData.toolDesc"
            maxlength="300"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="指令Logo" prop="maskIcon">
          <el-select v-model="saveOrEditData.maskIcon" placeholder="请选择图标">
            <el-option
              v-for="item in maskIconList"
              :key="item"
              :label="item"
              :value="item">
              <el-row >
                <el-col :span="12">
                  <div :class="'t-maskicon '+item"></div>
                </el-col>
                <el-col :span="12">{{ item }}</el-col>
              </el-row>>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input
            type="textarea"
            placeholder="如有备注请输入描述"
            v-model="saveOrEditData.comment"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>
        <!-- 按钮组 -->
        <div style="margin-top: 10px">
          <el-button type="primary" @click="submitSaveOrEdit" :loading="formLoading">确定</el-button>
          <el-button
            @click="
							dialog.saveOrEditDiv = false;
              dialog.isEdit = false;
							resetAddOrEditOrShowForm();
						"
          >
            取消
          </el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog
      title="面具指令详情"
      :visible.sync="dialog.showDiv"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-descriptions class="margin-top" :column="2" border>
        <el-descriptions-item label="面具名称">{{ showData.name }}</el-descriptions-item>
        <el-descriptions-item label="面具类型">{{ showData.gptMaskTypeName }}</el-descriptions-item>
        <el-descriptions-item label="指令内容" span="2">{{ showData.toolDesc }}</el-descriptions-item>
        <el-descriptions-item label="面具LOGO">
          <div :class="'t-maskicon '+showData.maskIcon"></div>
        </el-descriptions-item>
        <el-descriptions-item label="备注">{{ showData.comment }}</el-descriptions-item>
      </el-descriptions>
      <el-button
        @click="
							dialog.showDiv = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>



  </div>
</template>

<script>
import * as axiosAPI from '@/api/gpt/mask.js';
import {maskiconsArr} from '@/assets/iconfont/maskicons.js';
import gd_param from "@/components/mycomponents/gd_param.vue";
import gd_file from "@/components/mycomponents/gd_file.vue";
export default {
  name: 'maskIndex',
  components:{gd_file, gd_param},
  data() {
    return {
      maskIconList:maskiconsArr,
      //查询表单数据
      queryForm: {
        name:null,
        gptMaskTypeCode:null
      },
      //表格数据
      tableData: [],
      //主表-分页数据
      pageData: {
        page: 1,
        size: 10,
        total: 0
      },
      //当前表格复选数据
      nowTableSelected: [],
      //弹窗状态
      dialog: {
        isEdit: false,
        saveOrEditDiv: false,
        showDiv: false
      },
      //添加编辑表单-检验
      saveOrEditDataFormRules:{
        maskIcon: { required: true, message: '请选择面具LOGO', trigger: 'change' },
        name: { required: true, message: '请输入面具名称', trigger: 'blur' },
        toolDesc: { required: true, message: '请输入指令内容', trigger: 'blur' },
        gptMaskTypeCode: { required: true, message: '请选择指令类型', trigger: 'change' }
      },
      saveOrEditData:{
        maskID:null,
        comment:null,
        name:null,
        maskIcon:null,
        toolDesc:null,
        gptMaskTypeCode:null,
        gptMaskTypeName:null
      },
      //展示表单数据
      showData: {
        maskID:null,
        comment:null,
        name:null,
        maskIcon:null,
        toolDesc:null,
        gptMaskTypeCode:null,
        gptMaskTypeName:null
      },
      //表单加载值
      formLoading: false
    };
  },
  mounted() {
    this.getTableDatas();
  },
  filters:{
    timeFormat:function(value){
      let date = new Date(value);
      let year = date.getFullYear();
      let month = date.getMonth() + 1 +'';
      let day = date.getDate() +'';
      let hour = date.getHours() +'';
      let minute = date.getMinutes() +'';
      let second = date.getSeconds() +'';
      return `${year}-${month.padStart(2,'0')}-${day.padStart(2,'0')} ${hour.padStart(2,'0')}:${minute.padStart(2,'0')}:${second.padStart(2,'0')}`
    }
  },
  computed:{
    getTagType(){
      return(data) =>{
        if(data == "1"){
          return "success";
        }else if(data == "2"){
          return "info";
        }else if(data == "3"){
          return "warning";
        }else{
          return "ppp";
        }
      }
    }
  },
  methods: {

    //查询表格数据
    getTableDatas() {
      let _this = this;
      axiosAPI.pages(_this.queryForm, _this.pageData)
        .then(function(response) {
          console.log(response);
          if (response.code == '0000') {
            _this.tableData = response.data.rows;
            _this.pageData.total = response.data.total;
          }
        });
    },
    //重置查询条件表单
    resetQueryForm() {
      this.queryForm = {
        name:null,
        gptMaskTypeCode:null
      };
      this.getTableDatas();
    },
    //重置添加、编辑、展示表单
    resetAddOrEditOrShowForm() {
      this.showData = {
        maskID:null,
        comment:null,
        name:null,
        maskIcon:null,
        toolDesc:null,
        gptMaskTypeCode:null,
        gptMaskTypeName:null
      };
      this.saveOrEditData = {
        maskID:null,
        comment:null,
        name:null,
        maskIcon:null,
        toolDesc:null,
        gptMaskTypeCode:null,
        gptMaskTypeName:null
      }
    },
    //提交-添加、编辑表单
    submitSaveOrEdit() {
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          let _this = this;
          if(_this.dialog.isEdit == true){// 编辑

            axiosAPI.update(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.dialog.isEdit = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });

          }else{ // 添加
            axiosAPI.add(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.dialog.isEdit = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });
          }
        }
      });
    },
    //提交-删除
    submitDelete(ids){
      let _this = this;
      axiosAPI.deletes(ids)
        .then(res => {
          if (res.code == '0000') {
            _this.getTableDatas();
          }
        });
    },
    //分页条目数切换
    tableSizeChange(size) {
      this.pageData.size = size;
      this.getTableDatas();
    },
    //页码切换
    tableCurrentChange(page) {
      this.pageData.page = page;
      this.getTableDatas();
    },
    //行-查看
    rowShow(data) {
      var _this = this;
      axiosAPI.getByID(data.maskID)
        .then(function(res){
          if(res.code == "0000"){
            _this.showData = res.data;
            _this.dialog.showDiv = true;
          }
        });
    },
    //行-编辑
    rowEdit(data) {

      let _this = this;
      axiosAPI.getByID(data.maskID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.isEdit = true;
            _this.dialog.saveOrEditDiv = true;
          }
        });
    },
    //行-删除
    rowDelete(data) {
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(data.maskID);
      });
    },
    //点击批量删除
    moreDeletes() {
      if(this.nowTableSelected.length<=0){
        this.$notify.warning({
          title: '删除提示',
          message: '请至少选择一条数据再删除'
        });
        return;
      }
      let ids = '';

      for (var i in this.nowTableSelected) {
        ids += this.nowTableSelected[i].maskID + ',';
      }

      ids = ids.substring(0, ids.length - 1);

      //调用删除
      this.submitDelete(ids);
    },
    //表格批量选择
    tableDataSelected(data){
      this.nowTableSelected = data;
    },
    // 面具类型选择
    gptMaskTypeChange(data){
      if(data == undefined){
        this.saveOrEditData.gptMaskTypeCode = null;
        this.saveOrEditData.gptMaskTypeName = null;
      }else{
        this.saveOrEditData.gptMaskTypeCode = data.dataValue;
        this.saveOrEditData.gptMaskTypeName = data.dataName;
      }
    }


  }
};
</script>

<style scoped></style>
